
 <!DOCTYPE HTML>
<html >
<head>
  <meta charset="UTF-8">
  
    <title>设计模式----代理模式详解 | Typhoon&#39;s code space</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
    
    <meta name="author" content="typhoon">
    

    
    <meta name="description" content="代理模式是Java常见的设计模式之一。所谓代理模式是指客户端并不直接调用实际的对象，而是通过调用代理，来间接的调用实际的对象。">
<meta name="keywords" content="设计模式">
<meta property="og:type" content="article">
<meta property="og:title" content="设计模式----代理模式详解">
<meta property="og:url" content="http://scorpioaeolus.oschina.io/2018/04/21/代理模式/index.html">
<meta property="og:site_name" content="Typhoon&#39;s code space">
<meta property="og:description" content="代理模式是Java常见的设计模式之一。所谓代理模式是指客户端并不直接调用实际的对象，而是通过调用代理，来间接的调用实际的对象。">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/04/21/代理模式/d1.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/04/21/代理模式/d2.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/04/21/代理模式/d3.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/04/21/代理模式/d4.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/04/21/代理模式/d5.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/04/21/代理模式/d6.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/04/21/代理模式/webchat.jpg">
<meta property="og:updated_time" content="2018-04-21T08:35:53.854Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="设计模式----代理模式详解">
<meta name="twitter:description" content="代理模式是Java常见的设计模式之一。所谓代理模式是指客户端并不直接调用实际的对象，而是通过调用代理，来间接的调用实际的对象。">
<meta name="twitter:image" content="http://scorpioaeolus.oschina.io/typhoon/2018/04/21/代理模式/d1.png">

    
    <link rel="alternative" href="/atom.xml" title="Typhoon&#39;s code space" type="application/atom+xml">
    
    
    <link rel="icon" href="/typhoon/img/java.png">
    
    
    <link rel="apple-touch-icon" href="/typhoon/img/jacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/typhoon/img/jacman.jpg">
    
    <link rel="stylesheet" href="/typhoon/css/style.css">
</head>

  <body>
    <header>
      
<div>
		
			<div id="imglogo">
				<a href="/typhoon/"><img src="/typhoon/img/java.png" alt="Typhoon&#39;s code space" title="Typhoon&#39;s code space"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/typhoon/" title="Typhoon&#39;s code space">Typhoon&#39;s code space</a></h1>
				<h2 class="blog-motto"></h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="Menu">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/typhoon/">主页</a></li>
					
						<li><a href="/typhoon/archives">归档</a></li>
					
						<li><a href="/typhoon/about">关于</a></li>
					
					<li>
 					
					<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="search" id="search" name="q" autocomplete="off" maxlength="20" placeholder="Search" />
						<input type="hidden" name="q" value="site:scorpioaeolus.oschina.io">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>
    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
  
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/typhoon/2018/04/21/代理模式/" title="设计模式----代理模式详解" itemprop="url">设计模式----代理模式详解</a>
  </h1>
  <p class="article-author">By
       
		<a href="/typhoon/about" title="typhoon" target="_blank" itemprop="author">typhoon</a>
		
  <p class="article-time">
    <time datetime="2018-04-21T06:44:08.000Z" itemprop="datePublished"> Published 2018-04-21</time>
    
  </p>
</header>
	<div class="article-content">
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">Contents</strong>
		
			<ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-静态代理"><span class="toc-number">1.</span> <span class="toc-text">1.静态代理</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#2-动态代理"><span class="toc-number">1.1.</span> <span class="toc-text">2.动态代理</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#总结"><span class="toc-number">2.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#创作不易，请多多支持！"><span class="toc-number">3.</span> <span class="toc-text">创作不易，请多多支持！</span></a></li></ol>
		
		</div>
		
		<pre><code>代理模式是Java常见的设计模式之一。所谓代理模式是指客户端并不直接调用实际的对象，而是通过调用代理，来间接的调用实际的对象。
</code></pre>  <a id="more"></a>
<pre><code>举个例子，你新到杭州来上班，需要找到住处，但是人生地不熟无法直接找到一手房源，这时通过小广告或者网络平台找到中介，你联系中介，
中介帮你找到房源并帮你租到了房子。这就是很典型的代理模式
</code></pre><img src="/typhoon/2018/04/21/代理模式/d1.png" alt="pattern" title="pattern">
<pre><code>图中上边是非代理模式，下边就是代理模式。

java语言中代理模式有两种实现方式：静态代理和动态代理，接下来将逐个分析。
</code></pre><h5 id="1-静态代理"><a href="#1-静态代理" class="headerlink" title="1.静态代理"></a>1.静态代理</h5><pre><code>就拿租房为例，定义一个租房的接口：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">package com.typhoon.javapattern.proxy;</div><div class="line"></div><div class="line">/**</div><div class="line"> *  租房能力接口</div><div class="line"> *  </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2018-04-21 15:11 Saturday</div><div class="line"> * @since V1.0.0</div><div class="line"> */</div><div class="line">public interface Rente &#123;</div><div class="line">	</div><div class="line">	/**</div><div class="line">	 *  租房</div><div class="line">	 */</div><div class="line">	void renteHouse();</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>真实的房东：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">package com.typhoon.javapattern.proxy;</div><div class="line"></div><div class="line">/**</div><div class="line"> *  房东</div><div class="line"> *  </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2018-04-21 15:13 Saturday</div><div class="line"> * @since V1.0</div><div class="line"> */</div><div class="line">public class Landlord implements Rente &#123;</div><div class="line"></div><div class="line">	@Override</div><div class="line">	public void renteHouse() &#123;</div><div class="line">		System.out.println(&quot;从房东手里租房&quot;);</div><div class="line">	&#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>中介（代理）:
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line">package com.typhoon.javapattern.proxy;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 中介</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2018-04-21 15:15 Saturday</div><div class="line"> * @since V1.0</div><div class="line"> */</div><div class="line">public class Intermediary implements Rente &#123;</div><div class="line"></div><div class="line">	/**</div><div class="line">	 *  真实租房能力</div><div class="line">	 */</div><div class="line">	private Rente rente;</div><div class="line">	</div><div class="line">	public Intermediary(Rente rente) &#123;</div><div class="line">		super();</div><div class="line">		this.rente = rente;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	@Override</div><div class="line">	public void renteHouse() &#123;</div><div class="line">		System.out.println(&quot;中介租房预处理&quot;);</div><div class="line">		if(null == rente) &#123;</div><div class="line">			throw new IllegalArgumentException(&quot;rente没有初始化&quot;);</div><div class="line">		&#125;</div><div class="line">		this.rente.renteHouse();</div><div class="line">		System.out.println(&quot;中介租房善后&quot;);</div><div class="line"></div><div class="line">	&#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>租客（消费者）：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">package com.typhoon.javapattern.proxy;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 租客(迷茫的你)</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2018-04-21 15:20 Saturday</div><div class="line"> * @since V1.0.0</div><div class="line"> */</div><div class="line">public class TenantClient &#123;</div><div class="line"></div><div class="line">	/**</div><div class="line">	 * 初到宝地租房</div><div class="line">	 * </div><div class="line">	 * @param args</div><div class="line">	 */</div><div class="line">	public static void main(String[] args) &#123;</div><div class="line">		//在网上找到中介</div><div class="line">		Intermediary proxy = new Intermediary(new Landlord());</div><div class="line"></div><div class="line">		//租房</div><div class="line">		proxy.renteHouse();</div><div class="line">	&#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>依赖关系如图：
</code></pre><img src="/typhoon/2018/04/21/代理模式/d2.png" alt="pattern" title="pattern">
<pre><code>运行TenantClient看到结果：
</code></pre><img src="/typhoon/2018/04/21/代理模式/d3.png" alt="pattern" title="pattern">
<pre><code>通过上面的代理代码，我们可以看出代理模式的特点，代理类接受一个Rente接口的对象，任何实现该接口的对象，都可以通过代理类进行代理，增加了通用性。
但是缺点很明显，每一个代理类都必须实现一遍委托类（也就是Landlord）的接口，如果接口增加方法，则代理类也必须跟着修改。其次，
代理类每一个接口对象对应一个委托对象，如果委托对象非常多，或者方法特别多，则静态代理类就非常臃肿，无法适用复杂的企业应用。
</code></pre><h6 id="2-动态代理"><a href="#2-动态代理" class="headerlink" title="2.动态代理"></a>2.动态代理</h6><pre><code>动态代理是借助java的反射机制动态创建代理类，java体系中常用的有两种实现方式：jdk自带和CGLIB

1）jdk动态代理
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">Rente rente = new Landlord();</div><div class="line">Rente proxy = (Rente)Proxy.newProxyInstance(rente.getClass().getClassLoader(),rente.getClass().getInterfaces(), new InvocationHandler() &#123;</div><div class="line">	</div><div class="line">	@Override</div><div class="line">	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable &#123;</div><div class="line">		System.out.println(&quot;动态代理预处理,method:&quot; + method.getName());</div><div class="line">		Object result = method.invoke(rente, args);</div><div class="line">		System.out.println(&quot;动态代理善后,method:&quot; + method.getName());</div><div class="line">		return result;</div><div class="line">	&#125;</div><div class="line">&#125;);</div><div class="line">proxy.renteHouse();</div></pre></td></tr></table></figure>
<pre><code>Proxy是jdk提供的动态代理生成类，newProxyInstance有三个参数，第一个是被代理类的类加载器，第二个是一个数组，就是被代理的接口，第三个是处理器。
运行客户端类：
</code></pre><img src="/typhoon/2018/04/21/代理模式/d4.png" alt="pattern" title="pattern">
<pre><code>可以看到已经打印出了我们想要的结果。这时候rente加一个方法：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">package com.typhoon.javapattern.proxy;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 收水费</div><div class="line"> * @author Typhoon</div><div class="line"> *</div><div class="line"> */</div><div class="line">public interface WaterFee &#123;</div><div class="line">	void collectWaterFee();</div><div class="line">&#125;</div><div class="line"></div><div class="line">/**</div><div class="line"> *  房东</div><div class="line"> *  </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2018-04-21 15:13 Saturday</div><div class="line"> * @since V1.0</div><div class="line"> */</div><div class="line">public class Landlord implements Rente,WaterFee &#123;</div><div class="line"></div><div class="line">	@Override</div><div class="line">	public void renteHouse() &#123;</div><div class="line">		System.out.println(&quot;从房东手里租房&quot;);</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	@Override</div><div class="line">	public void collectWaterFee() &#123;</div><div class="line">		System.out.println(&quot;房东收水费&quot;);</div><div class="line">		</div><div class="line">	&#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>再次运行客户端类：
</code></pre><img src="/typhoon/2018/04/21/代理模式/d5.png" alt="pattern" title="pattern">
<pre><code>可以看到，被代理的类增加了功能，并不需要修改代理来的逻辑，这也是动态代理类的魅力所在。
但是jdk有一个缺点，就是无法动态代理没有实现接口的类，也就是无法代理简单类，但是在真是业务场景中我们想要代理和扩充的不止接口实现类，
所以这一点有些场景是无法接受的。还好Cglib能够实现这一点。

2）Cglib动态代理
Cglib代理,也叫作子类代理,它是在内存中构建一个子类对象从而实现对目标对象功能的扩展。
Cglib非jdk提供，所以需要引入依赖：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;dependency&gt;</div><div class="line">	&lt;groupId&gt;cglib&lt;/groupId&gt;</div><div class="line">	&lt;artifactId&gt;cglib&lt;/artifactId&gt;</div><div class="line">	&lt;version&gt;2.2.2&lt;/version&gt;</div><div class="line">&lt;/dependency&gt;</div></pre></td></tr></table></figure>
<pre><code>新建简单类Hello：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">/**</div><div class="line"> * 未实现接口的简单类</div><div class="line"> *  </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2018-04-21 16:18 Saturday</div><div class="line"> */</div><div class="line">public class Hello &#123;</div><div class="line"></div><div class="line">	public void sayHello(String name) &#123;</div><div class="line">		System.out.println(&quot;hello! dear &quot; + name);</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>动态代理生成类：
</code></pre><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div></pre></td><td class="code"><pre><div class="line">package com.typhoon.javapattern.proxy;</div><div class="line"></div><div class="line">import java.lang.reflect.Method;</div><div class="line"></div><div class="line">import net.sf.cglib.proxy.Enhancer;</div><div class="line">import net.sf.cglib.proxy.MethodInterceptor;</div><div class="line">import net.sf.cglib.proxy.MethodProxy;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 代理生成类</div><div class="line"> * </div><div class="line"> * @author Typhoon</div><div class="line"> * @date 2018-04-21 16:21 Saturday</div><div class="line"> */</div><div class="line">public class CglibProxy implements MethodInterceptor &#123;</div><div class="line">	</div><div class="line">	private Object target;</div><div class="line">	</div><div class="line">	public CglibProxy(Object target) &#123;</div><div class="line">		super();</div><div class="line">		this.target = target;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	@Override</div><div class="line">	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable &#123;</div><div class="line">		System.out.println(&quot;Cglib 代理预处理,method:&quot; + method.getName());</div><div class="line">		Object result = method.invoke(target, args);</div><div class="line">		System.out.println(&quot;Cglib 代理善后,method:&quot; + method.getName());</div><div class="line">		return result;</div><div class="line">	&#125;</div><div class="line">	</div><div class="line">	public Object getDynamicProxyInstance() &#123;</div><div class="line">		//1.工具类</div><div class="line">        Enhancer en = new Enhancer();</div><div class="line">        //2.设置父类</div><div class="line">        en.setSuperclass(target.getClass());</div><div class="line">        //3.设置回调函数</div><div class="line">        en.setCallback(this);</div><div class="line">        //4.创建子类(代理对象)</div><div class="line">        return en.create();</div><div class="line">	&#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>运行客户端：
</code></pre><img src="/typhoon/2018/04/21/代理模式/d6.png" alt="pattern" title="pattern">
<pre><code>可以看到我们创建的简单类也能够被代理了。
</code></pre><h5 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h5><pre><code>    代理模式算是应用最广泛的设计模式之一，在各种框架中都有高频的应用，spring aop，事务，dubbo等等。其核心理念满足开闭原则，
    对功能修改关闭，对功能扩展和提升开放。基本应用场景是在不触动原功能的基础上做一些额外的处理，
    比如记录日志，缓存，权限过滤等等。

此篇暂且分析到这里，希望能帮大家对代理模式带来更深刻的理解和认识，从而更好更安全的使用代理模式！
</code></pre><h5 id="创作不易，请多多支持！"><a href="#创作不易，请多多支持！" class="headerlink" title="创作不易，请多多支持！"></a>创作不易，请多多支持！</h5><pre><code>附带公众号：
</code></pre><img src="/typhoon/2018/04/21/代理模式/webchat.jpg" alt="pattern" title="pattern">
  
	</div>
		<footer class="article-footer clearfix">
<div class="article-catetags">

<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/typhoon/categories/设计模式/">设计模式</a>
</div>


  <div class="article-tags">
  
  <span></span> <a href="/typhoon/tags/设计模式/">设计模式</a>
  </div>

</div>



	<div class="article-share" id="share">
	
	  <div data-url="http://scorpioaeolus.oschina.io/2018/04/21/代理模式/" data-title="设计模式----代理模式详解 | Typhoon&#39;s code space" data-tsina="null" class="share clearfix">
	  </div>
	
	</div>


</footer>

   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/typhoon/2018/05/20/dubbo泛化实现与引用/" title="dubbo泛化实现与引用">
  <strong>上一篇：</strong><br/>
  <span>
  dubbo泛化实现与引用</span>
</a>
</div>


<div class="next">
<a href="/typhoon/2018/04/21/单例模式/"  title="设计模式----单例模式详解">
 <strong>下一篇：</strong><br/> 
 <span>设计模式----单例模式详解
</span>
</a>
</div>

</nav>

	


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="Show Sidebar"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">Contents</strong>
 
 <ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-静态代理"><span class="toc-number">1.</span> <span class="toc-text">1.静态代理</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#2-动态代理"><span class="toc-number">1.1.</span> <span class="toc-text">2.动态代理</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#总结"><span class="toc-number">2.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#创作不易，请多多支持！"><span class="toc-number">3.</span> <span class="toc-text">创作不易，请多多支持！</span></a></li></ol>
 
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="Hide Sidebar"></a></div>
<aside class="clearfix">

  


  
<div class="categorieslist">
	<p class="asidetitle">Categories</p>
		<ul>
		
		  
			<li><a href="/typhoon/categories/dubbo/" title="dubbo">dubbo<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java性能优化/" title="java性能优化">java性能优化<sup>17</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java源码分析/" title="java源码分析">java源码分析<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/motan/" title="motan">motan<sup>2</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/spring/" title="spring">spring<sup>11</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springboot/" title="springboot">springboot<sup>28</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springmvc/" title="springmvc">springmvc<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/容器/" title="容器">容器<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/工具/" title="工具">工具<sup>7</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/性能优化/" title="性能优化">性能优化<sup>5</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/架构/" title="架构">架构<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/案例分析/" title="案例分析">案例分析<sup>13</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/缓存/" title="缓存">缓存<sup>4</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/面试题/" title="面试题">面试题<sup>1</sup></a></li>
		  
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">Tags</p>
		<ul class="clearfix">
		
			
				<li><a href="/typhoon/tags/springboot/" title="springboot">springboot<sup>28</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/java性能优化/" title="java性能优化">java性能优化<sup>9</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/限流/" title="限流">限流<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring5/" title="Spring5">Spring5<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/案例分析/" title="案例分析">案例分析<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/性能优化/" title="性能优化">性能优化<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring/" title="Spring">Spring<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo/" title="dubbo">dubbo<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/分页/" title="分页">分页<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/并发/" title="并发">并发<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/异常拦截/" title="异常拦截">异常拦截<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo分布式事务/" title="dubbo分布式事务">dubbo分布式事务<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/自带图片/" title="自带图片">自带图片<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/svn导出mvn项目/" title="svn导出mvn项目">svn导出mvn项目<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo过滤器/" title="dubbo过滤器">dubbo过滤器<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/排序性能对比/" title="排序性能对比">排序性能对比<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/ListenableFuture/" title="ListenableFuture">ListenableFuture<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/PropertyChangeListener/" title="PropertyChangeListener">PropertyChangeListener<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/jdbcTemplate高可用/" title="jdbcTemplate高可用">jdbcTemplate高可用<sup>1</sup></a></li>
			
		
		</ul>
</div>


  <div class="linkslist">
  <p class="asidetitle">Links</p>
    <ul>
        
          <li>
            
            	<a href="https://coderq.com" target="_blank" title="一个面向程序员交流分享的新一代社区">码农圈</a>
            
          </li>
        
          <li>
            
            	<a href="http://wuchong.me" target="_blank" title="Jark&#39;s Blog">Jark&#39;s Blog</a>
            
          </li>
        
    </ul>
</div>

  


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS</a>
</div>

  <div class="weiboshow">
  <p class="asidetitle">Weibo</p>
    <iframe width="100%" height="119" class="share_self"  frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=119&fansRow=2&ptype=1&speed=0&skin=9&isTitle=1&noborder=1&isWeibo=0&isFans=0&uid=null&verifier=b3593ceb&dpc=1"></iframe>
</div>


</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hello ,I&#39;m a java coder. <br/>
			This is my blog,believe it or not.</p>
	</section>
	 
	<div class="social-font" class="clearfix">
		
		
		
		
		
		
		
		
		
		
		<a href="mailto:ScorpioAeolus@163.com" target="_blank" class="icon-email" title="Email Me"></a>
		
	</div>
			
		

		<p class="copyright">
		Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wuchong/jacman" target="_blank" title="Jacman">Jacman</a> © 2019 
		
		<a href="/typhoon/about" target="_blank" title="typhoon">typhoon</a>
		
		
		</p>
</div>
</footer>
    <script src="/typhoon/js/jquery-2.0.3.min.js"></script>
<script src="/typhoon/js/jquery.imagesloaded.min.js"></script>
<script src="/typhoon/js/gallery.js"></script>
<script src="/typhoon/js/jquery.qrcode-0.12.0.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
  
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else{
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  c.click(function(){
    ta.css('display', 'block').addClass('fadeIn');
  });
  o.click(function(){
    ta.css('display', 'none');
  });
  $(window).scroll(function(){
    ta.css("top",Math.max(140,320-$(this).scrollTop()));
  });
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina'),
      description = $this.attr('description');
  var html = [
  '<div class="hoverqrcode clearfix"></div>',
  '<a class="overlay" id="qrcode"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="微信"></a>',
  '<a href="http://widget.renren.com/dialog/share?resourceUrl=' + encodedUrl + '&srcUrl=' + encodedUrl + '&title=' + title +'" class="article-share-renren" target="_blank" title="人人"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="微博"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);

  $('.hoverqrcode').hide();

  var myWidth = 0;
  function updatehoverqrcode(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
    var qrsize = myWidth > 1024 ? 200:100;
    var options = {render: 'image', size: qrsize, fill: '#2ca6cb', text: url, radius: 0.5, quiet: 1};
    var p = $('.article-share-qrcode').position();
    $('.hoverqrcode').empty().css('width', qrsize).css('height', qrsize)
                          .css('left', p.left-qrsize/2+20).css('top', p.top-qrsize-10)
                          .qrcode(options);
  };
  $(window).resize(function(){
    $('.hoverqrcode').hide();
  });
  $('.article-share-qrcode').click(function(){
    updatehoverqrcode();
    $('.hoverqrcode').toggle();
  });
  $('.article-share-qrcode').hover(function(){}, function(){
      $('.hoverqrcode').hide();
  });
});   
</script>









<link rel="stylesheet" href="/typhoon/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/typhoon/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.article-content').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox')) return;
      var alt = this.alt;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'article' + i);
    });
  });
  if($.fancybox){
    $('.fancybox').fancybox();
  }
}); 
</script>



<!-- Analytics Begin -->



<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?e6d1f421bbc9962127a50488f9ed37d1";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>



<!-- Analytics End -->

<!-- Totop Begin -->

	<div id="totop">
	<a title="Back to Top"><img src="/typhoon/img/scrollup.png"/></a>
	</div>
	<script src="/typhoon/js/totop.js"></script>

<!-- Totop End -->

<!-- MathJax Begin -->
<!-- mathjax config similar to math.stackexchange -->


<!-- MathJax End -->

<!-- Tiny_search Begin -->

<!-- Tiny_search End -->

  </body>
</html>
